package kr.co.casually;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class Me2day extends Activity {

	private WebView web;
	private ProgressDialog progressDialog;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.me2day);

		Intent intent = getIntent();
		String keyword = intent.getStringExtra("keyword");
		String tag = intent.getStringExtra("tag");
		
		web = (WebView) findViewById(R.id.m2day_webview);

		web.setVerticalScrollBarEnabled(false);
		web.setHorizontalScrollBarEnabled(false);
		web.getSettings().setDefaultTextEncodingName("UTF-8");
		web.getSettings().setJavaScriptEnabled(true);
		
		web.loadUrl("http://me2day.net/plugins/mobile_post/new?new_post[body]=" + keyword + "&new_post[tags]="+tag);
		web.setWebViewClient(new WebViewClient() {

//			// 로딩 프로그래스 다이얼로그
//			public void onLoadResource(WebView view, String url) {
//				if (progressDialog == null) {
//					progressDialog = new ProgressDialog(Me2day.this);
//					progressDialog.setMessage("잠시만 기다려 주세요.");
//					progressDialog.show();
//				}
//			}

			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				if (progressDialog == null) {
					progressDialog = new ProgressDialog(Me2day.this);
					progressDialog.setMessage("잠시만 기다려 주세요.");
					progressDialog.show();
				}
			}
			
			public void onPageFinished(WebView view, String url) {
					progressDialog.dismiss();
					progressDialog = null;
			}

			@Override
			public void onReceivedError(WebView view, int errorCode,
					String description, String failingUrl) {
				Toast.makeText(Me2day.this, "페이지 로딩 오류:" + description,
						Toast.LENGTH_SHORT).show();
			}

		});
		
//		web.setWebChromeClient(new WebChromeClient() {
//		    @Override
//		    public boolean onJsAlert(WebView view, String url, String message, final android.webkit.JsResult result)
//		    {
//		        new AlertDialog.Builder(Me2day.this)
//		            .setMessage(message)
//		            .setPositiveButton(android.R.string.ok,
//		                    new AlertDialog.OnClickListener()
//		                    {
//		                        public void onClick(DialogInterface dialog, int which)
//		                        {
//		                            //result.confirm();
//		                            //web.loadUrl("http://me2day.net/n/account/login");
//		                        }
//		                    })
//		            .setCancelable(false)
//		            .create()
//		            .show();
//
//		        return true;
//		    };
//		});		
		
	}// oncreate
	
	@Override
	protected void onRestart() {
		super.onRestart();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
			web.goBack();
			return true;
		} else {
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}

}
